<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
	<script type="text/javascript">
		
		$(function(){
			
			//自定义互换两个节点的函数
			function replaceEach(aNode, bNode){
				//1. 获取 aNode 和 bNode 的父节点. 使用 parentNode 属性
				var aParent = aNode.parentNode;
				var bParent = bNode.parentNode;
				
				if(aParent && bParent){
					//2. 克隆 aNode 或 bNode
					var aNode2 = aNode.cloneNode(true);
					
					//克隆 aNode 的同时, 把 onclick 事件也复制. 
					aNode2.onclick = aNode.onclick;
					
					//克隆 aNode 的同时, 把 onclick 事件也复制. 
					aNode2.index = aNode.index;
					
					//3. 分别调用 aNode 的父节点和 bNode 的父节点的 replaceChild()
					//方法实现节点的互换
					bParent.replaceChild(aNode2, bNode);
					aParent.replaceChild(bNode, aNode);
				}
			}
			
			$("li").each(function(index){
				this.index = index;
				
				$(this).click(function(){
					
					var targetIndex = 0;
					if(this.index < 4){
						targetIndex = this.index + 4;
					}else{
						targetIndex = this.index - 4;
					}

					var targetNode = $("li")[targetIndex];
					
					/*
					var $temp = $(this).clone(true);
					$temp[0].index = this.index;
					
					//index 属性也实现互换
					
					$(targetNode).replaceWith($temp);
					$temp[0].index = targetNode.index;
					*/
					
					alert(this.index);
					alert(targetIndex);
					
					var tempIndex = this.index;
					this.index = $("li")[targetIndex].index;
					$("li")[targetIndex].index = tempIndex
					
					replaceEach(this, $("li")[targetIndex]);
					
				});
			});
			
		})
		
	
	</script>
</head>
<body>

	<p>你喜欢哪个城市?</p>
	
		<ul id="city">
			<li id="bj">北京</li>
			<li>上海</li>
			<li>东京</li>
			<li>首尔</li>
		</ul>
		
		<br><br>
		<p>你喜欢哪款单机游戏?</p>
		<ul id="game">
			<li id="rl">红警</li>
			<li>实况</li>
			<li>极品飞车</li>
			<li>魔兽</li>
		</ul>
		
		<br><br>
		
		<form action="dom-7.html" name="myform">
			
			<input type="radio" name="type" value="city">城市
			<input type="radio" name="type" value="game">游戏
		
			name: <input type="text" name="name"/>
		
			<input type="submit" value="Submit" id="submit"/>
			
		</form>


</body>
</html>